﻿using DocumentFormat.OpenXml.Lite.Core.Office;
using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Drawing;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Base;
using DocumentFormat.OpenXml.Lite.SpreadsheetML.Excel.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DML = DocumentFormat.OpenXml.Lite.DrawingML;
using DMLG = DocumentFormat.OpenXml.Lite.DrawingML.Geometry;
using DMLT = DocumentFormat.OpenXml.Lite.DrawingML.Text;
using DMLV = DocumentFormat.OpenXml.Lite.DrawingML.Visual;

namespace Samples.Excel
{
    class Images
    {
        public static void Create()
        {
            Workbook wb = DocumentManager.Instance.CreateSpreadsheet();

            Worksheet sheet1 = wb.CreateWorksheet("Sheet1", null);
                        
            Image image;
            using (var ms = new System.IO.MemoryStream())
            {
                Resources.force.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                image = new Image(ms.GetBuffer(), ImageType.JPG);
            }

            WorksheetDrawing wd = sheet1.CreateDrawing();
            string relationshipId = wd.AddPicture(image);
            wd.CellAnchors.Add(new TwoCellAnchor()
            {
                EditAs = ST_EditAs.OneCell,
                From = new Point()
                {
                    Column = new Text<int>(1),
                    ColumnOffset = new Text<long>(100),
                    Row = new Text<int>(1),
                    RowOffset = new Text<long>(100)
                },
                To = new Point()
                {
                    Column = new Text<int>(6),
                    ColumnOffset = new Text<long>(100),
                    Row = new Text<int>(20),
                    RowOffset = new Text<long>(100)
                },
                ClientData = new ClientData(),
                Graphic = new Picture()
                {
                    NonVisualProperties = new NonVisualPictureProperties
                    {
                        NonVisualProperties = new DMLG.Properties.NonVisualProperties()
                        {
                            Name = "Picture1",
                            Id = (uint)new Random().Next(100000)
                        },
                        NonVisualPictureDrawingProperties = new DMLG.Properties.NonVisualPictureDrawingProperties()
                    },
                    BlipFill = new DMLV.BlipFill()
                    {
                        Blip = new DMLT.Blip()
                        {
                            EmbeddedPictureReference = relationshipId
                        },
                        Element = new DMLV.Stretch()
                    },
                    ShapeProperties = new DMLG.Properties.ShapeProperties()
                    {
                        Transform2d = new DML.Transformation.Transform2d()
                        {
                            Offset = new DMLG.Offset() { X = 0, Y = 0 },
                            Extents = new DMLG.Extent() 
                            {
                                ExtentLength = 420000,
                                ExtentWidth = 520000
                            }
                        },
                        Geometry = new DML.Transformation.PresetGeometry()
                        {
                            ShapeType = DML.Types.ST_ShapeType.Rectangle,
                            AdjustValues = new DMLG.AdjustValueList()
                        },
                    }
                }
            });

            wb.Save(@"samples\image.xlsx");

            Workbook parsed = null;
            if (DocumentManager.Instance.OpenExcelDocument(@"samples\image.xlsx", out parsed))
            {
                Image getBack = parsed.FindElementFor<Image>(
                    parsed.Worksheets[0].DrawingElement, relationshipId);
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}
